package com.weatherquery.servlet;

import com.weatherquery.informbean.WeatherData;
import com.weatherquery.service.WeatherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Natural-Pride
 * @Date 2023/12/18 20:04
 * @注释
 */

//使用@WebServlet注解来定义一个 web服务,将其映射到"/WeatherQueryServlet" URL
@WebServlet("/WeatherQueryServlet")
public class WeatherQueryServlet extends HttpServlet{
    //重写 doGet方法,该方法在 HTTP GET请求到该 Servlet时被调用
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //调用 doPost方法来处理 HTTP POST请求
        doPost(request, response);
    }
    //重写 doPost方法,该方法在 HTTP POST请求到该 Servlet时被调用
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //1. 接收点击修改之后传递过来的城市ID
        String cityId = request.getParameter("cityId");
        //调用 WeatherService的 getWeather方法来获取该城市天气信息
        WeatherService weatherService = new WeatherService();
        WeatherData weatherUpdate = weatherService.getWeather(cityId);
        // 将该修改之前的城市天气信息传递到"weatherModify.jsp"页面进行显示
        request.setAttribute("weatherUpdate", weatherUpdate);
        request.getRequestDispatcher("weatherModify.jsp").forward(request, response);
    }
}
